﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using XiAnAirport.BUS;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class FaultRepairController : ControllerBase
    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public FaultRepairController(IHttpContextAccessor contextAccessor, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }

        /// <summary>
        /// 新增故障类别信息
        /// </summary>
        /// <param name="faultRepairType">故障类别信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddFaultRepairType")]
        public ResponseMessage AddFaultRepairType(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.AddFaultRepairType(faultRepairType);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增故障类别信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 编辑故障类别信息
        /// </summary>
        /// <param name="faultRepairType">故障类别信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateFaultRepairType")]
        public ResponseMessage UpdateFaultRepairType(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.EditFaultRepairType(faultRepairType);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改故障类别信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改故障类别信息异常";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除故障类别信息
        /// </summary>
        /// <param name="faultRepairType">故障类别信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelFaultRepairType")]
        public ResponseMessage DelFaultRepairType(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.DelFaultRepairType(faultRepairType);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除故障类别信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }

            return message;
        }

        /// <summary>
        /// 查询故障类别信息
        /// </summary>
        /// <param name="faultRepairType">故障类别信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryFaultRepairTypeList")]
        public ResponseMessage QueryFaultRepairTypeList(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                FaultRepairBUS BUS = new FaultRepairBUS();
                message = BUS.QueryFaultRepairType(faultRepairType);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入故障类别信息
        /// </summary>
        /// <param name="faultRepairType">故障类别信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadFaultRepairType")]
        public ResponseMessage LoadFaultRepairType(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.LoadFaultRepairType(faultRepairType);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入故障类别信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 清理故障类别信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("CleanUpData")]
        public ResponseMessage CleanUpData()
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.CleanUpRepairTyper();
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "清理故障类别信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "清理故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清理故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }

            return message;
        }


        /// <summary>
        /// 批量删除故障类别信息
        /// </summary>
        /// <param name="ids">故障类别id</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelFaultRepairType")]
        public ResponseMessage BatchDelFaultRepairType(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairBUS BUS = new FaultRepairBUS();
            try
            {
                message = BUS.BatchDelFaultRepairType(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除故障类别信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }

            return message;
        }

        /// <summary>
        /// 查询故障类别信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAllFaultReason")]
        public ResponseMessage QueryAllFaultReason()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                FaultRepairBUS BUS = new FaultRepairBUS();
                message = BUS.QueryAllFaultReason();
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询故障类别信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障类别";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
